﻿using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using TrafficArchives.Common;

namespace TrafficArchives.DataAccess
{
    public class ValueObjectClassDb : DbBase
    {
        public override PaginationData<DataTable> GetData(Request request)
        {
            var paginationData = new PaginationData<DataTable>();
            var normalSql = "select t.*, rownum as rowno from valueobjectclass t where " + request.ToSql();
            var sql = request.Pagination.Enabled
                          ? string.Format("select * from ({0}) A where A.rowno > {1} and A.rowno <= {2}", normalSql,
                                          GetPageStart(request.Pagination), GetPageEnd(request.Pagination))
                          : normalSql;
            paginationData.Data = ExecuteDataTable(sql);
            if (request.Pagination.Enabled)
            {
                paginationData.RecordCount = ExecuteCount(normalSql.Replace("t.*, rownum as rowno", "count(*)"));
            }
            return paginationData;
        }

        /// <summary>
        /// 根据Id单条数据。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override DataTable GetData(string id)
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "valueobjectclass_id",
                Operator = ConditionOperator.Equal,
                Values = new object[] { id }
            });
            return GetData(request).Data;
        }

        public DataTable GetData()
        {
            return GetData(new Request()).Data;
        }

        /// <summary>
        /// 取表结构。
        /// </summary>
        /// <returns></returns>
        public override DataTable GetNoneData()
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "valueobjectclass_id",
                Operator = ConditionOperator.Equal,
                Values = new object[] { "null" }
            });
            return GetData(request).Data;
        }

        public override void Add(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "insert into VALUEOBJECTCLASS(VALUEOBJECTCLASS_ID,CODE,NAME,REMARK)" +
                               "values(:VALUEOBJECTCLASS_ID,:CODE,:NAME,:REMARK)";
            var parameters = new[]{
                                    MakeParam(":VALUEOBJECTCLASS_ID", OracleType.VarChar, 40, "VALUEOBJECTCLASS_ID"),
                                     MakeParam(":CODE", OracleType.VarChar, 40, "CODE"),
                                     MakeParam(":NAME", OracleType.VarChar, 40, "NAME"),
                                     MakeParam(":REMARK", OracleType.VarChar, 40, "REMARK")
                                  };
            OnAdd(data, sql, parameters);
        }

        public override void Update(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "update VALUEOBJECTCLASS set "+
                               "CODE=:CODE" +
                               ",NAME=:NAME" +
                               ",REMARK=:REMARK where VALUEOBJECTCLASS_ID =:VALUEOBJECTCLASS_ID";
            var parameters = new[]{
                                     MakeParam(":CODE", OracleType.VarChar, 10, "CODE"),
                                     MakeParam(":NAME", OracleType.VarChar, 40, "NAME"),
                                     MakeParam(":REMARK", OracleType.VarChar, 40, "REMARK"),
                                     MakeParam(":VALUEOBJECTCLASS_ID", OracleType.VarChar, 40, "VALUEOBJECTCLASS_ID")
                                 };
            OnUpdate(data, sql, parameters);
        }

        public override void Delete(List<string> ids)
        {
            const string sql = "delete from VALUEOBJECTCLASS t where t.VALUEOBJECTCLASS_ID in ('{0}')";
            ExecuteNoneQuery(string.Format(sql, string.Join("','", ids)));
        }
    }
}